﻿// Necroz project team © <2015> <ArchLord2EMU>
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
// 
// Author:Sagara
// Date:2015-09-06-23:29

using System;
using AL2Login.Interfaces;
using AL2Login.Network.Frames.Send;
using ArchLord2Commons.IoC;
using ArchLord2Commons.Models;
using ArchLord2Commons.Network;
using NLog;

namespace AL2Login.Network
{
    public class Al2Connection : AALClient
    {
        private static readonly Logger Log = LogManager.GetLogger(typeof(Al2Connection).Name);

        public AccountData AccountInfo { get; set; }

        protected override void OnDisconnected()
        {
            if(AccountInfo != null)
               Global.GetMember<IGsPipeService>().DisconnectMemberOnGameServer(AccountInfo.Id, AccountInfo.ServerId);
        }

        protected override void OnConnected()
        {
            new SfinitilizeConnection().Send(this);
        }

        protected override void FrameReceived(AALNetFrame frame)
        {
            var prRes = false;

            try
            {
                prRes = frame.ProcessRead();
            }
            catch (Exception ex)
            {
                Log.Error("Read for frame '{0}' cause an exception and frame process will be skipped.\n{1}",
                    frame.GetType().Name, ex);
            }

            if (prRes)
                frame.PassToEngine(this);
        }
    }
}
